package com.ruoyi.web.controller.finance;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.finance.domain.Payment;
import com.ruoyi.finance.service.IPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 付款Controller
 * 
 * @author liuyu
 * @date 2024-11-04
 */
@RestController
@RequestMapping("/finance/payment")
public class PaymentController extends BaseController
{
    @Autowired
    private IPaymentService paymentService;

    /**
     * 查询付款列表
     */
    @PreAuthorize("@ss.hasPermi('payment:payment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Payment payment)
    {
        startPage();
        List<Payment> list = paymentService.selectPaymentList(payment);
        return getDataTable(list);
    }


    /**
     * 获取付款详细信息
     */
    @PreAuthorize("@ss.hasPermi('payment:payment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(paymentService.selectPaymentById(id));
    }

    /**
     * 新增付款
     */
    @PreAuthorize("@ss.hasPermi('payment:payment:add')")
    @Log(title = "付款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Payment payment)
    {
        return toAjax(paymentService.insertPayment(payment));
    }

    /**
     * 修改付款
     */
    @PreAuthorize("@ss.hasPermi('payment:payment:edit')")
    @Log(title = "付款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Payment payment)
    {
        return toAjax(paymentService.updatePayment(payment));
    }



    /**
     * 根据id获取付款记录
     * @param id
     * @return
     */
    @GetMapping("byId")
    public AjaxResult getPaymentById(Long id){
        return success(paymentService.getBillingById(id));
    }


    /**
     * 获取付款总额
     * @param accountsPayableId 关联应付表id
     * @return
     */
    @GetMapping("/accountsPayableId")
    public AjaxResult getSumAmount(@RequestParam("accountsPayableId") Long accountsPayableId){
        return success(paymentService.selectSumAmount(accountsPayableId));
    }
}
